package com.example.springbootshiro.controller;

import com.example.springbootshiro.model.User;
import com.example.springbootshiro.response.BusinessError;
import com.example.springbootshiro.response.CommonReturnData;
import com.example.springbootshiro.response.GlobalCodeEnum;
import com.example.springbootshiro.service.UserService;
import com.example.springbootshiro.shiro.token.TokenObject;
import com.example.springbootshiro.util.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YangGang on 2021/11/23 15:16。
 */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserService userService;

    /**
     * 登陆
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/in")
    public Object in(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken authenticationToken = new UsernamePasswordToken(username, password);
        try {
            subject.login(authenticationToken);
        } catch (UnknownAccountException e) {
            throw new BusinessError("用户名不存在");
        } catch (IncorrectCredentialsException e) {
            throw new BusinessError("用户名或密码错误");
        } catch (BusinessError e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessError("登陆异常，请稍后再试");
        }

        TokenObject userToken = new TokenObject();
        userToken.setUsername(username);
        userToken.setPassword(password);
        String token = JwtUtil.createToken(userToken);
        User user = userService.findUserByUsername(username);
        user.setSalt(null);
        user.setPassword(null);
        //返回
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("user", user);
        return CommonReturnData.success(data);
    }


    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    public Object register(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.addUser(user);
        return CommonReturnData.success(user.getUserId());
    }


    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/out")
    public Object out() {
        SecurityUtils.getSubject().logout();
        return CommonReturnData.success();
    }

    /**
     * 无权限
     *
     * @return
     */
    @RequestMapping("/unauthorized")
    public Object unauthorized() {
        return CommonReturnData.create(null, GlobalCodeEnum.NOT_PERMISSION);
    }


}
